// ****************************************************************************
// *     Filename:  MySD.cc
// *  Description:  Sensitive Detector
// *      License:  Geant4 Software License 
// *                http://geant4.cern.ch/license/LICENSE.html
// *       Author:  Tran Hoai Nam (nam), nam@kuno-g.phys.sci.osaka-u.ac.jp
// * Organization:  Osaka University
// *      Created:  2012-09-14 13:04:12
// ****************************************************************************
#include "MySD.hh"
#include "MyHit.hh"

#include "G4HCofThisEvent.hh"
#include "G4Step.hh"
#include "G4SDManager.hh"
#include "G4TouchableHistory.hh"
#include "G4Track.hh"
#include "G4VProcess.hh"
#include "G4UnitsTable.hh"
#include "G4ios.hh"

//Constructor: insert detector name into collectionName, set hit code id to -1
MySD::MySD(const G4String & name) : G4VSensitiveDetector(name)
{
	collectionName.insert(name);
	fHCID = -1;
	fEdep = 0.;
}

MySD::~MySD()
{;}

//Initialize the SD: create hit collection; get pointer to the SD from
//SDManager; add the hit collection to hitCollectionOfThisEvent
void MySD::Initialize(G4HCofThisEvent * aHCE) 
{
	fpHC = new MyHitsCollection(SensitiveDetectorName, collectionName[0]);
	if (fHCID<0)
		fHCID = G4SDManager::GetSDMpointer()->GetCollectionID(fpHC);
	aHCE->AddHitsCollection(fHCID,fpHC);
}

G4bool MySD::ProcessHits(G4Step *aStep, G4TouchableHistory *aHistory) 
{
	G4double edep = aStep->GetTotalEnergyDeposit();
	if (edep==0.) return false;
	MyHit *aHit = new MyHit();
	aHit->Set_edep(edep);
	//aHit->Print();
	fEdep += edep;

	return true;
}

void MySD::EndOfEvent(G4HCofThisEvent *aHCE) 
{
	G4cout<<"Total edep: "<<fEdep<<G4endl;
}
